﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SLPropertyGrid
{
    public class ContentControlWithTemplateSelector : ContentControl
    {
        public static readonly DependencyProperty ContentTemplateSelectorProperty =
            DependencyProperty.Register("ContentTemplateSelector", typeof(DataTemplateSelector),
            typeof(ContentControlWithTemplateSelector), new PropertyMetadata(null, OnContentTemplateSelectorChanged));

        private static void OnContentTemplateSelectorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((ContentControlWithTemplateSelector)d).OnContentTemplateSelectorChanged((DataTemplateSelector)e.NewValue, (DataTemplateSelector)e.NewValue);
        }
        protected virtual void OnContentTemplateSelectorChanged(DataTemplateSelector oldContentTemplateSelector, DataTemplateSelector newContentTemplateSelector)
        {
        }

        public DataTemplateSelector ContentTemplateSelector
        {
            get
            {
                return (DataTemplateSelector)base.GetValue(ContentTemplateSelectorProperty);
            }
            set
            {
                base.SetValue(ContentTemplateSelectorProperty, value);
            }
        }

        public ContentControlWithTemplateSelector()
        {
            this.Loaded += new RoutedEventHandler(OnLoaded);
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            ExploreVisualTree();
        }


        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            DataTemplate dataTemplate = ChooseTemplate(this.Content, this);
            Property p = this.Content as Property;
            
            if (!p.CanWrite)
                this.Visibility = Visibility.Collapsed;
            
            this.ContentTemplate = dataTemplate;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

        private DataTemplate ChooseTemplate(object p, FrameworkElement element)
        {
            if (ContentTemplateSelector != null)
            {
                return ContentTemplateSelector.SelectTemplate(p, element);
            }
            return null;
        }


        public void ExploreVisualTree()
        {
            IList<DependencyObject> descendants = Helper.ExploreVisualTree(this, o => ((FrameworkElement)o).Name == "default");
            foreach (var o in descendants)
            {
                FrameworkElement fe = (FrameworkElement)o;
                fe.BindingValidationError += new EventHandler<ValidationErrorEventArgs>(fe_BindingValidationError);
            }
        }

        void fe_BindingValidationError(object sender, ValidationErrorEventArgs e)
        {
            Control c = sender as Control;
            if (c != null)
            {
                if (e.Action == ValidationErrorEventAction.Added)
                {
                    c.Background = new SolidColorBrush(Colors.Red);
                }
                else if (e.Action == ValidationErrorEventAction.Removed)
                {
                    c.Background = new SolidColorBrush(Colors.White);
                }

            }
        }
    }
}
